package com.sky.zzh.setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sky.widget.SHToast;
import com.sky.zzh.R;
import com.sky.zzh.SHApplication;
import com.sky.zzh.util.KeyboardUtil;
import com.sky.zzh.util.KeyboardUtil.InputFinishListener;
import com.sky.zzh.util.UserInfoManager;

/**
 * 设置app密码
 * 
 * @author skypan
 * 
 */
public class SetAppPwdActivity extends Activity {

	private LinearLayout layout_input;
	private KeyboardUtil keyBoard;
	private Button mBtnBack;
	private TextView mTvTitle,mTvBar;
	private boolean isSecond = false;
	private boolean isIn = false; 
	private String pwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_set_pwd);

		mTvTitle = (TextView) findViewById(R.id.label_title);   
		mTvBar = (TextView) findViewById(R.id.tv_title);
		pwd = getIntent().getStringExtra("pwd");
		isSecond = getIntent().getBooleanExtra("second", false);
		isIn = getIntent().getBooleanExtra("isIn", false);
		if(isSecond){
			mTvTitle.setText("请确认密码");
		}else{
			if(isIn){
				mTvBar.setText("输入密码");
			}else{
				mTvTitle.setText("请输入APP密码");
			}
		}
		layout_input = (LinearLayout) findViewById(R.id.layout_input);
		mBtnBack = (Button) findViewById(R.id.btn_back);
		mBtnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(isIn){
					moveTaskToBack(true);
				}else{
					finish();
				}
			}
		});
		keyBoard = new KeyboardUtil(this, this, layout_input, new InputFinishListener() {

			@Override
			public void inputHasOver(String text) {
//				Toast.makeText(getActivity(), "输入完成:" + text, Toast.LENGTH_LONG).show();
				keyBoard.hideKeyboard();
				if(!isSecond){
					if(isIn){
						if(text.equals(UserInfoManager.getInstance().getAppPwd())){
							SHApplication.getInstance().isLocked = false;
							finish();
						}else{
							SHToast.showToast(SetAppPwdActivity.this, "密码错误");
						}
					}else{
						Intent intent = new Intent(SetAppPwdActivity.this,SetAppPwdActivity.class);
						intent.putExtra("pwd", text);
						intent.putExtra("second", true);
						startActivity(intent);
						finish();
					}
				}else{
					if(!pwd.equals(text)){
						SHToast.showToast(SetAppPwdActivity.this, "密码不一致");
						return;
					}
					UserInfoManager.getInstance().setAppPwd(text);
					UserInfoManager.getInstance().sync(SetAppPwdActivity.this, true);
					finish();
				}
			}
		});
		layout_input.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				keyBoard.showKeyboard();
				return false;
			}
		});
	
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		if(isIn){
			moveTaskToBack(true);
		}else{
			finish();
		}
	}
	
	

}
